package com.ruoyi.fileMaintenance.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import cn.xfyun.api.TtsClient;
import cn.xfyun.model.response.TtsResponse;
import cn.xfyun.service.tts.AbstractTtsWebSocketListener;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.fileMaintenance.domain.TChineseMeaning;
import com.ruoyi.fileMaintenance.domain.TCourseLearning;
import com.ruoyi.fileMaintenance.domain.dto.TLearningMaterialsDto;
import com.ruoyi.fileMaintenance.xfyunApi.TtsClientApp;
import okhttp3.Response;
import okhttp3.WebSocket;
import org.springframework.stereotype.Service;
import com.ruoyi.fileMaintenance.mapper.TLearningMaterialsMapper;
import com.ruoyi.fileMaintenance.domain.TLearningMaterials;
import com.ruoyi.fileMaintenance.service.ITLearningMaterialsService;

import javax.annotation.Resource;

/**
 * 学习资料Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-27
 */
@Service
public class TLearningMaterialsServiceImpl implements ITLearningMaterialsService {
    @Resource
    private TLearningMaterialsMapper tLearningMaterialsMapper;

    /**
     * 查询学习资料
     *
     * @param id 学习资料主键
     * @return 学习资料
     */
    @Override
    public TLearningMaterials selectTLearningMaterialsById(Long id) {
        TLearningMaterials tLearningMaterials = tLearningMaterialsMapper.selectTLearningMaterialsById(id);
        // 通过资料id查所有词性
        List<TChineseMeaning> tChineseMeaningList = tLearningMaterialsMapper.selectTChineseMeaningList(id);
        tLearningMaterials.setPartOfSpeechSelectList(tChineseMeaningList);
        return tLearningMaterials;
    }

    /**
     * 查询学习资料列表
     *
     * @param tLearningMaterials 学习资料
     * @return 学习资料
     */
    @Override
    public List<TLearningMaterials> selectTLearningMaterialsList(TLearningMaterials tLearningMaterials) {
        return tLearningMaterialsMapper.selectTLearningMaterialsList(tLearningMaterials);
    }

    /**
     * app查询学习资料列表
     *
     * @param tLearningMaterialsDto 学习资料
     * @return 学习资料集合
     */
    @Override
    public List<TLearningMaterials> selectTLearningMaterialsList2(TLearningMaterialsDto tLearningMaterialsDto) {
        tLearningMaterialsDto.setUserId(SecurityUtils.getUserId());
        int type = tLearningMaterialsDto.getType();
        List<TLearningMaterials> list = new ArrayList<>();
        if (type == 2) { // 已学
            list = tLearningMaterialsMapper.selectLearnedTLearningMaterialsList(tLearningMaterialsDto);
        } else if (type == 3) {// 未学
            list = tLearningMaterialsMapper.selectUnlearnedTLearningMaterialsList(tLearningMaterialsDto);
        } else {
            list = tLearningMaterialsMapper.selectAllTLearningMaterialsList(tLearningMaterialsDto);
        }
        return list;
    }

    /**
     * app 随机查课程下未学单词 1条数据
     *
     * @param courseId 课程id
     * @return 学习资料
     */
    @Override
    public TLearningMaterials selectUnlearnedTLearningMaterials(Long courseId) {
        TLearningMaterialsDto tLearningMaterialsDto = new TLearningMaterialsDto();
        tLearningMaterialsDto.setCourseId(courseId);
        tLearningMaterialsDto.setUserId(SecurityUtils.getUserId());
        return tLearningMaterialsMapper.selectUnlearnedTLearningMaterials(tLearningMaterialsDto);
    }

    /**
     * app 单词测试
     *
     * @param tLearningMaterials 学习资料
     * @return 学习资料集合
     */
    public List<TLearningMaterials> learningMaterialsTest(TLearningMaterials tLearningMaterials) {
        List<TLearningMaterials> tLearningMaterialsList = tLearningMaterialsMapper.selectTLearningMaterialsTest(tLearningMaterials);
        for (TLearningMaterials tLearningMaterials1 : tLearningMaterialsList) {
            Long id = tLearningMaterials1.getId();
            List<TLearningMaterials> tLearningMaterialsList2 = tLearningMaterialsMapper.selectRandomTLearningMaterials(id);
            tLearningMaterials1.setOptionsList(tLearningMaterialsList2);
        }
        return tLearningMaterialsList;
    }

    /**
     * 新增学习资料
     *
     * @param tLearningMaterials 学习资料
     * @return 结果
     */
    @Override
    public int insertTLearningMaterials(TLearningMaterials tLearningMaterials) {
        // 生成语音文件
        tLearningMaterials.setVoiceFilesPath(TtsClientApp.LanguageSynthesis(tLearningMaterials.getEnglish()));
        tLearningMaterials.setCreateBy(SecurityUtils.getUsername());
        tLearningMaterials.setCreateTime(DateUtils.getNowDate());
        int i = tLearningMaterialsMapper.insertTLearningMaterials(tLearningMaterials);
        // 新增词性
        insertTChineseMeaning(tLearningMaterials);
        return i;
    }

    /**
     * 修改学习资料
     *
     * @param tLearningMaterials 学习资料
     * @return 结果
     */
    @Override
    public int updateTLearningMaterials(TLearningMaterials tLearningMaterials) {
        // 生成语音文件
        tLearningMaterials.setVoiceFilesPath(TtsClientApp.LanguageSynthesis(tLearningMaterials.getEnglish()));
        tLearningMaterials.setUpdateBy(SecurityUtils.getUsername());
        tLearningMaterials.setUpdateTime(DateUtils.getNowDate());
        int i = tLearningMaterialsMapper.updateTLearningMaterials(tLearningMaterials);
        // 新增词性
        insertTChineseMeaning(tLearningMaterials);
        return i;
    }

    /**
     * 新增词性
     *
     * @param tLearningMaterials 学习资料
     * @return 结果
     */
    public void insertTChineseMeaning(TLearningMaterials tLearningMaterials) {
        // 获取学习资料id
        long learningMaterialsId = tLearningMaterials.getId();
        // 删除学习资料id所有词性
        tLearningMaterialsMapper.deleteTChineseMeaningById(learningMaterialsId);
        List<TChineseMeaning> tChineseMeaningList = tLearningMaterials.getPartOfSpeechSelectList();
        for (TChineseMeaning tChineseMeaning : tChineseMeaningList) {
            tChineseMeaning.setLearningMaterialsId(learningMaterialsId);
            tChineseMeaning.setCreateBy(SecurityUtils.getUsername());
            tChineseMeaning.setCreateTime(DateUtils.getNowDate());
        }
        tLearningMaterialsMapper.insertTChineseMeaning(tChineseMeaningList);
    }

    /**
     * 批量删除学习资料
     *
     * @param ids 需要删除的学习资料主键
     * @return 结果
     */
    @Override
    public int deleteTLearningMaterialsByIds(Long[] ids) {
        return tLearningMaterialsMapper.deleteTLearningMaterialsByIds(ids);
    }

    /**
     * 删除学习资料信息
     *
     * @param id 学习资料主键
     * @return 结果
     */
    @Override
    public int deleteTLearningMaterialsById(Long id) {
        return tLearningMaterialsMapper.deleteTLearningMaterialsById(id);
    }

    /**
     * 新增学习情况
     *
     * @param tCourseLearning 学习情况
     * @return 结果
     */
    @Override
    public int insertCourseLearning(TCourseLearning tCourseLearning) {
        tCourseLearning.setUserId(SecurityUtils.getUserId());
        return tLearningMaterialsMapper.insertCourseLearning(tCourseLearning);
    }
}
